package com.love.marriage.defaultAmountConfigRecord.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.common.enums.OperationTypeEnum;
import com.love.marriage.defaultAmountConfig.vo.DefaultAmountConfigVo;
import com.love.marriage.defaultAmountConfigRecord.entity.DefaultAmountConfigRecord;
import com.love.marriage.defaultAmountConfigRecord.mapper.DefaultAmountConfigRecordMapper;
import com.love.marriage.defaultAmountConfigRecord.service.DefaultAmountConfigRecordService;
import com.love.marriage.defaultAmountConfigRecord.vo.DefaultAmountConfigRecordVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * description:  默认金额配置操作记录表服务实现类
 * author:       system
 * createDate:   2024-12-07
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class DefaultAmountConfigRecordServiceImpl extends ServiceImpl<DefaultAmountConfigRecordMapper, DefaultAmountConfigRecord> implements DefaultAmountConfigRecordService {

    private final DefaultAmountConfigRecordMapper defaultAmountConfigRecordMapper;

    @Override
    public Page<DefaultAmountConfigRecordVo> getPage(Long pageNum, Long pageSize, DefaultAmountConfigRecordVo recordVo) {
        Page<DefaultAmountConfigRecordVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return defaultAmountConfigRecordMapper.getPage(page, recordVo);
    }

    @Override
    public DefaultAmountConfigRecordVo queryDefaultAmountConfigRecord(Long id) {
        return defaultAmountConfigRecordMapper.queryDefaultAmountConfigRecord(id);
    }

    @Override
    public Page<DefaultAmountConfigRecordVo> getPageByConfigId(Long configId, Long pageNum, Long pageSize) {
        Page<DefaultAmountConfigRecordVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        DefaultAmountConfigRecordVo recordVo = new DefaultAmountConfigRecordVo();
        recordVo.setConfigId(configId);
        return defaultAmountConfigRecordMapper.getPage(page, recordVo);
    }

    @Override
    public void recordInsertOperation(DefaultAmountConfigVo newConfig, String operationReason) {
        DefaultAmountConfigRecord record = new DefaultAmountConfigRecord();
        record.setConfigId(newConfig.getId());
        record.setOperationType(OperationTypeEnum.INSERT.getCode());
        record.setNewConfigName(newConfig.getConfigName());
        record.setNewConfigCode(newConfig.getConfigCode());
        record.setNewAmount(newConfig.getAmount());
        record.setNewDescription(newConfig.getDescription());
        record.setOperationReason(operationReason != null ? operationReason : "新增默认金额配置");
        
        defaultAmountConfigRecordMapper.insert(record);
    }

    @Override
    public void recordUpdateOperation(DefaultAmountConfigVo oldConfig, DefaultAmountConfigVo newConfig, String operationReason) {
        DefaultAmountConfigRecord record = new DefaultAmountConfigRecord();
        record.setConfigId(newConfig.getId());
        record.setOperationType(OperationTypeEnum.UPDATE.getCode());
        
        // 设置操作前的值
        record.setOldConfigName(oldConfig.getConfigName());
        record.setOldConfigCode(oldConfig.getConfigCode());
        record.setOldAmount(oldConfig.getAmount());
        record.setOldDescription(oldConfig.getDescription());
        
        // 设置操作后的值
        record.setNewConfigName(newConfig.getConfigName());
        record.setNewConfigCode(newConfig.getConfigCode());
        record.setNewAmount(newConfig.getAmount());
        record.setNewDescription(newConfig.getDescription());
        record.setOperationReason(operationReason != null ? operationReason : "修改默认金额配置");
        
        defaultAmountConfigRecordMapper.insert(record);
    }

    @Override
    public void recordDeleteOperation(DefaultAmountConfigVo oldConfig, String operationReason) {
        DefaultAmountConfigRecord record = new DefaultAmountConfigRecord();
        record.setConfigId(oldConfig.getId());
        record.setOperationType(OperationTypeEnum.DELETE.getCode());
        record.setOldConfigName(oldConfig.getConfigName());
        record.setOldConfigCode(oldConfig.getConfigCode());
        record.setOldAmount(oldConfig.getAmount());
        record.setOldDescription(oldConfig.getDescription());
        record.setOperationReason(operationReason != null ? operationReason : "删除默认金额配置");
        
        defaultAmountConfigRecordMapper.insert(record);
    }
}